//
//  UIViewEx.swift
//  Mall-swift
//
//  Created by Bob on 2020/10/18.
//  Copyright © 2020 Bob. All rights reserved.
//

import UIKit
import SnapKit

extension UIView {
    /// 半高的圆角
    public func makeCircular() {
        layer.cornerRadius = bounds.height / 2.0
        layer.masksToBounds = true
    }
    /// 设置圆角
    public var corner: CGFloat {
        get {
            return layer.cornerRadius
        }
        set(newCorner) {
            if layer.cornerRadius != newCorner {
                layer.cornerRadius = newCorner
            }
            if !layer.masksToBounds {
                layer.masksToBounds = true
            }
        }
    }
    
    
    func embed(view: UIView) {
        addSubview(view)
        view.snp.makeConstraints{$0.edges.equalToSuperview()}
    }
    
    
   public var LL_Top: CGFloat {
        set(new){
            var f = frame
            f.origin.y = new
            frame = f
        }
        get{
            return frame.minY
        }
    }
    
    
    public var LL_Bottom: CGFloat {
        set(new){
            var f = frame
            f.origin.y = new - frame.size.height
            frame = f
        }
        
        get{
            return frame.origin.y + frame.size.height
        }
    }
    
    
    public var LL_Left: CGFloat {
        set(new){
            var f = frame
            f.origin.x = new
            frame = f
        }
        
        get{
            return frame.maxY
        }
    }
    
    
    public var LL_right: CGFloat {
        set(new){
            var f = frame
            f.origin.x = new - frame.size.width
            frame = f
        }
        
        get{
            return frame.origin.x + frame.size.width
        }
    }
    
    
    
    public var LL_centerX: CGFloat {
        set(new){
            var f = frame
            f.origin.x = new - f.width*0.5
            frame = f
        }
        get {
            return center.x
        }
    }
    
    
    
    public var LL_centerY: CGFloat {
        set(new) {
            var f = frame
            f.origin.y = new - f.height*0.5
            frame = f
        }
        get {
            return center.y
        }
    }
    
    public  var LL_width: CGFloat {
        set(new) {
            var f = frame
            f.size.width = new
            frame = f
        }
        get {
            return frame.width
        }
    }
    
    public var LL_height: CGFloat {
        set(new) {
            var f = frame
            f.size.height = new
            frame = f
        }
        get {
            return frame.height
        }
    }
    
    
    //获取UIView所在的视图控制器
    public func getFirstViewController()->UIViewController?{
        
        for view in sequence(first: self.superview, next: {$0?.superview}){
            
            if let responder = view?.next{
                
                if responder.isKind(of: UIViewController.self){
                    
                    return responder as? UIViewController
                }
            }
        }
        return nil
    }
    
    /// 获取UIView所在的视图控制器
    public var viewController: UIViewController? {
        var parent: UIResponder? = self
        while parent != nil {
            parent = parent?.next
            if let viewController = parent as? UIViewController {
                return viewController
            }
        }
        return nil
    }
}
